package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.ucity.ui.ReleaseBenefitActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.ClubAccessBean;
import com.uchengwang.ui.cm_ui.umsg.bean.ClubAccessListBean;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/6/16.
 * 社团纳新 展示页 请求网络判断是否发布过社团纳新活动
 */

public class ClubAccessInfoActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private RelativeLayout apply_rl, advise_rl;
    private LinearLayout all_ln, allInfo_ln, info_ln;
    private TextView applyNum, adviseNum, text, clubAccess_again;
    private ImageView cover;
    private Button bt;
    private AlertDialog dialog;
    private ClubAccessBean clubAccessBean;
    private String rightText = "";
    private RelativeLayout clubAccess_all;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_club_access_info);
        setTitle("社团纳新");

        initView();
    }

    private void initView (){
        clubAccess_all = (RelativeLayout) findViewById(R.id.clubAccess_all);

        all_ln = (LinearLayout) findViewById(R.id.clubAccess_all_ln);
        apply_rl = (RelativeLayout) findViewById(R.id.clubAccess_apply_rl);
        apply_rl.setOnClickListener(this);
        advise_rl = (RelativeLayout) findViewById(R.id.clubAccess_advise_rl);
        advise_rl.setOnClickListener(this);
        allInfo_ln = (LinearLayout) findViewById(R.id.clubAccess_allInfo_ln);
        allInfo_ln.setOnClickListener(this);
        clubAccess_again = (TextView) findViewById(R.id.clubAccess_again);//重新编辑
        clubAccess_again.setOnClickListener(this);

        info_ln = (LinearLayout) findViewById(R.id.clubAccess_info_ln);//纳新Cover 描述
        applyNum = (TextView) findViewById(R.id.clubAccess_applyNum);
        adviseNum = (TextView) findViewById(R.id.clubAccess_adviseNum);
        text = (TextView) findViewById(R.id.clubAccess_text);
        cover = (ImageView) findViewById(R.id.clubAccess_cover);
        bt = (Button) findViewById(R.id.clubAccess_bt);
        bt.setOnClickListener(this);

        getClubAccess();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseConfig.isRelease){
            getClubAccess();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        BaseConfig.isRelease = false;
    }

    /**
     * 获取本社团的社团纳新 没有发不过才可以发布纳新
     * */
    private void getClubAccess(){
        MapRequest mapRequest = new MapRequest();
        mapRequest.put(BaseConfig.PAGE, 1);
        mapRequest.put(BaseConfig.PERPAGE, 1);
        mapRequest.put(BaseConfig.ORG_ID, UCommonUtil.getUtil().getId());
        HttpManager.newInstance(this).query(ClubConfig.ClubAccess.method_get, ClubConfig.ClubAccess.URL, mapRequest, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                all_ln.setVisibility(View.INVISIBLE);
                UCommonUtil.getUtil().showToast(ClubAccessInfoActivity.this, "获取失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                clubAccess_all.setVisibility(View.VISIBLE);

                if (status != 0){
                    all_ln.setVisibility(View.INVISIBLE);
                    return;
                }
                if (!TextUtils.isEmpty(data)){
                    ClubAccessListBean bean = JsonUtils.getManager().stringToBean(data, ClubAccessListBean.class);
                    if (bean != null){
                        all_ln.setVisibility(View.VISIBLE);
                        info_ln.setVisibility(View.VISIBLE);
                        bt.setVisibility(View.GONE);

                        List<ClubAccessBean> list = bean.getData();
                        clubAccessBean = list.get(0);
                        openOrClose(clubAccessBean.isActive());

                        ImageManager.getInstance().loadImage(ClubAccessInfoActivity.this, cover, clubAccessBean.getCover(), ImageManager.ImageType.ROUND);

                        text.setText(clubAccessBean.getRecruitTitle());
                        bt.setVisibility(View.GONE);
                    }else {
                        //没有发布过社团纳新
                        info_ln.setVisibility(View.GONE);
                    }
                }else {
                    info_ln.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 显示关闭或者开启纳新
     * @param isOpen true 显示关闭 false 显示开启
     * */
    private void openOrClose(final boolean isOpen){
        rightText = "";
        if (isOpen){
            rightText = "关闭";
        }else {
            rightText = "开启";
        }

        setRightText(rightText, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isToOpen(isOpen);
            }
        });
    }

    /**
     * 请求网络关闭/开启纳新
     * */
    private void isToOpen(boolean isOpen){
        if (clubAccessBean == null){
            return;
        }
        LoadingUtils.getUtils().loading(this);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UPDATE_ID, clubAccessBean.getId());
        map.put(BaseConfig.IS_ACTIVI, !isOpen);
        HttpManager.newInstance(this).query(ClubConfig.ClubSwitch.method, ClubConfig.ClubSwitch.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                Log.d("jjf", "status="+status+"=msg="+msg);
                UCommonUtil.getUtil().showToast(ClubAccessInfoActivity.this, "失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (status != 0){
                    return;
                }
                UCommonUtil.getUtil().showToast(ClubAccessInfoActivity.this, rightText+"成功");
                clubAccessBean.setActive(!clubAccessBean.isActive());
                openOrClose(clubAccessBean.isActive());

            }
        });
    }




    @Override
    public void onClick(View view) {
        Intent intent = null;

        switch (view.getId()){
            case R.id.clubAccess_apply_rl:
                //查看纳新报名人数
                if (clubAccessBean == null){
                    return;
                }
                intent = new Intent(this, ClubAccessApplyNewActivity.class);
                intent.putExtra(BaseConfig.RECRUIT_ID, clubAccessBean.getId());
                intent.putExtra(BaseConfig.GROUP_ID, clubAccessBean.getRcGroupId());
                intent.putExtra(BaseConfig.GROUP_NAME_UCW, clubAccessBean.getRcGroupName());
                break;
            case R.id.clubAccess_advise_rl:
                //查看纳新留言建议
//                intent = new Intent(this, ClubAccessNoteActivity.class);
                break;
            case R.id.clubAccess_again://重新编辑
                if (clubAccessBean == null){
                    return;
                }

                dialog = DialogUtils.getUtils().showDialogAll(this, false, "重新编辑", "重新编辑会覆盖之前的数据", "取消", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.cancel();
                        Intent in = new Intent(ClubAccessInfoActivity.this, ReleaseBenefitActivity.class);
                        in.putExtra(BaseConfig.PUB_TYPE, BaseConfig.CLUB_NEW_MEMBER);
                        in.putExtra(BaseConfig.IS_AGAIN, true);//重新编辑
                        if (clubAccessBean != null){
                            in.putExtra(BaseConfig.RECRUIT_ID, clubAccessBean.getId());
                        }
                        startActivity(in);
                    }
                }, null);

                break;
            case R.id.clubAccess_allInfo_ln:
                if (clubAccessBean == null){
                    return;
                }
//                intent = new Intent(this, ClubAccessDetailActivity.class);
//                intent.putExtra(BaseConfig.RECRUIT_ID, clubAccessBean.getId());
//                intent.putExtra(BaseConfig.ORG_ID, clubAccessBean.getOrgId());
                break;

            case R.id.clubAccess_bt:
                intent = new Intent(this, ReleaseBenefitActivity.class);
                intent.putExtra(BaseConfig.PUB_TYPE, BaseConfig.CLUB_NEW_MEMBER);
                break;
        }

        if (intent != null){
            startActivity(intent);
        }
    }
}
